home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gimp / 2.0 / plug-ins / py-slice.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  12.4 KB  |  307 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. from gimpfu import *
  6. import os.path as os
  7. gettext.install('gimp20-python', gimp.locale_directory, unicode = True)
  8.  
  9. def pyslice(image, drawable, save_path, html_filename, image_basename, image_extension, separate, image_path, cellspacing, animate, skip_caps):
  10.     if animate:
  11.         count = 0
  12.         drw = []
  13.         for i in xrange(len(image.layers) - 1, -1, -1):
  14.             if image.layers[i].visible:
  15.                 drw.append(image.layers[i])
  16.                 count += 1
  17.                 if count == 3:
  18.                     break
  19.                 
  20.             count == 3
  21.         
  22.     
  23.     (vert, horz) = get_guides(image)
  24.     if len(vert) == 0 and len(horz) == 0:
  25.         return None
  26.     gimp.progress_init(_('Slice'))
  27.     progress_increment = 1 / ((len(horz) + 1) * (len(vert) + 1))
  28.     progress = 0
  29.     
  30.     def check_path(path):
  31.         path = os.path.abspath(path)
  32.         if not os.path.exists(path):
  33.             os.mkdir(path)
  34.         
  35.         return path
  36.  
  37.     save_path = check_path(save_path)
  38.     if not os.path.isdir(save_path):
  39.         save_path = os.path.dirname(save_path)
  40.     
  41.     if separate:
  42.         image_relative_path = image_path
  43.         if not image_relative_path.endswith('/'):
  44.             image_relative_path += '/'
  45.         
  46.         image_path = check_path(os.path.join(save_path, image_path))
  47.     else:
  48.         image_relative_path = ''
  49.         image_path = save_path
  50.     tw = TableWriter(os.path.join(save_path, html_filename), cellspacing = cellspacing, animate = animate)
  51.     top = 0
  52.     for i in range(0, len(horz) + 1):
  53.         if i == len(horz):
  54.             bottom = image.height
  55.         else:
  56.             bottom = image.get_guide_position(horz[i])
  57.         tw.row_start()
  58.         left = 0
  59.         for j in range(0, len(vert) + 1):
  60.             if j == len(vert):
  61.                 right = image.width
  62.             else:
  63.                 right = image.get_guide_position(vert[j])
  64.             if skip_caps:
  65.                 if len(horz) >= 2 or i == 0 or i == len(horz) or len(vert) >= 2:
  66.                     if j == 0 or j == len(vert):
  67.                         skip_stub = True
  68.                     else:
  69.                         skip_stub = False
  70.             if not animate or skip_stub:
  71.                 src = image_relative_path + slice(image, None, image_path, image_basename, image_extension, left, right, top, bottom, i, j, '')
  72.             else:
  73.                 src = []
  74.                 for layer, postfix in zip(drw, ('', 'hover', 'clicked')):
  75.                     src.append(image_relative_path + slice(image, layer, image_path, image_basename, image_extension, left, right, top, bottom, i, j, postfix))
  76.                 
  77.             tw.cell(src, right - left, bottom - top, i, j, skip_stub)
  78.             left = right + cellspacing
  79.             progress += progress_increment
  80.             gimp.progress_update(progress)
  81.         
  82.         tw.row_end()
  83.         top = bottom + cellspacing
  84.     
  85.     tw.close()
  86.  
  87.  
  88. def slice(image, drawable, image_path, image_basename, image_extension, left, right, top, bottom, i, j, postfix):
  89.     if postfix:
  90.         postfix = '_' + postfix
  91.     
  92.     src = '%s_%d_%d%s.%s' % (image_basename, i, j, postfix, image_extension)
  93.     filename = os.path.join(image_path, src)
  94.     if not drawable:
  95.         temp_image = image.duplicate()
  96.         temp_drawable = temp_image.active_layer
  97.     elif image.base_type == INDEXED:
  98.         original_active = image.active_layer
  99.         image.active_layer = drawable
  100.         temp_image = image.duplicate()
  101.         temp_drawable = temp_image.active_layer
  102.         image.active_layer = original_active
  103.         temp_image.disable_undo()
  104.         while len(temp_image.layers) > 1:
  105.             if temp_image.layers[0] != temp_drawable:
  106.                 pdb.gimp_image_remove_layer(temp_image, temp_image.layers[0])
  107.                 continue
  108.             pdb.gimp_image_remove_layer(temp_image, temp_image.layers[1])
  109.     else:
  110.         temp_image = pdb.gimp_image_new(drawable.width, drawable.height, image.base_type)
  111.         temp_drawable = pdb.gimp_layer_new_from_drawable(drawable, temp_image)
  112.         temp_image.add_layer(temp_drawable, -1)
  113.     temp_image.disable_undo()
  114.     temp_image.crop(right - left, bottom - top, left, top)
  115.     if image_extension == 'gif' and image.base_type == RGB:
  116.         pdb.gimp_image_convert_indexed(temp_image, NO_DITHER, MAKE_PALETTE, 255, True, False, False)
  117.     
  118.     if image_extension == 'jpg' and image.base_type == INDEXED:
  119.         pdb.gimp_image_convert_rgb(temp_image)
  120.     
  121.     pdb.gimp_file_save(temp_image, temp_drawable, filename, filename)
  122.     gimp.delete(temp_image)
  123.     return src
  124.  
  125.  
  126. class GuideIter:
  127.     
  128.     def __init__(self, image):
  129.         self.image = image
  130.         self.guide = 0
  131.  
  132.     
  133.     def __iter__(self):
  134.         return iter(self.next_guide, 0)
  135.  
  136.     
  137.     def next_guide(self):
  138.         self.guide = self.image.find_next_guide(self.guide)
  139.         return self.guide
  140.  
  141.  
  142.  
  143. def get_guides(image):
  144.     vguides = []
  145.     hguides = []
  146.     for guide in GuideIter(image):
  147.         orientation = image.get_guide_orientation(guide)
  148.         guide_position = image.get_guide_position(guide)
  149.         if guide_position > 0:
  150.             if orientation == ORIENTATION_VERTICAL:
  151.                 if guide_position < image.width:
  152.                     vguides.append((guide_position, guide))
  153.                 
  154.             elif orientation == ORIENTATION_HORIZONTAL:
  155.                 if guide_position < image.height:
  156.                     hguides.append((guide_position, guide))
  157.                 
  158.             
  159.         orientation == ORIENTATION_VERTICAL
  160.     
  161.     
  162.     def position_sort(x, y):
  163.         return cmp(x[0], y[0])
  164.  
  165.     vguides.sort(position_sort)
  166.     hguides.sort(position_sort)
  167.     vguides = [ g[1] for g in vguides ]
  168.     hguides = [ g[1] for g in hguides ]
  169.     return (vguides, hguides)
  170.  
  171.  
  172. class TableWriter:
  173.     
  174.     def __init__(self, filename, cellpadding = 0, cellspacing = 0, border = 0, animate = False):
  175.         self.filename = filename
  176.         self.table_attrs = { }
  177.         self.table_attrs['cellpadding'] = cellpadding
  178.         self.table_attrs['cellspacing'] = cellspacing
  179.         self.table_attrs['border'] = border
  180.         self.image_prefix = os.path.basename(filename)
  181.         self.image_prefix = self.image_prefix.split('.')[0]
  182.         self.image_prefix = self.image_prefix.replace('-', '_')
  183.         self.image_prefix = self.image_prefix.replace(' ', '_')
  184.         if animate:
  185.             self.animate = True
  186.             self.images = []
  187.         else:
  188.             self.animate = False
  189.         if os.path.exists(filename):
  190.             self.urls = self.parse_urls()
  191.         else:
  192.             self.urls = []
  193.         self.url_index = 0
  194.         self.html = open(filename, 'wt')
  195.         self.open()
  196.  
  197.     
  198.     def next_url(self):
  199.         if self.url_index < len(self.urls):
  200.             self.url_index += 1
  201.             return self.urls[self.url_index - 1]
  202.         return '#'
  203.  
  204.     
  205.     def write(self, s, vals = None):
  206.         if vals:
  207.             s = s % vals
  208.         
  209.         self.html.write(s + '\n')
  210.  
  211.     
  212.     def open(self):
  213.         out = "<!--HTML SNIPPET GENERATED BY GIMP\n\nWARNING!! This is NOT a fully valid HTML document, it is rather a piece of\nHTML generated by GIMP's py-slice plugin that should be embedded in an HTML\nor XHTML document to be valid.\n\nReplace the href targets in the anchor (<a >) for your URLS to have it working\nas a menu.\n -->\n"
  214.         out += '<table'
  215.         for attr, value in self.table_attrs.iteritems():
  216.             out += ' %s="%s"' % (attr, value)
  217.         
  218.         out += '>'
  219.         self.write(out)
  220.  
  221.     
  222.     def close(self):
  223.         self.write('</table>\n')
  224.         prefix = self.image_prefix
  225.         if self.animate:
  226.             out = '\n<script language="javascript" type="text/javascript">\n/* Made with GIMP */\n\n/* Preload images: */\n    images_%s = new Array();\n                   \n' % prefix
  227.             for image in self.images:
  228.                 for type_ in ('plain', 'hover', 'clicked'):
  229.                     if image.has_key(type_):
  230.                         image_index = '%d_%d_%s' % (image['index'][0], image['index'][1], type_)
  231.                         out += '    images_%s["%s"] = new  Image();\n' % (prefix, image_index)
  232.                         out += '    images_%s["%s"].src = "%s";\n' % (prefix, image_index, image[type_])
  233.                         continue
  234.                 
  235.             
  236.             out += '\nfunction exchange (image, images_array_name, event)\n  {\n    name = image.name;\n    images = eval (images_array_name);\n\n    switch (event)\n      {\n        case 0:\n          image.src = images[name + "_plain"].src;\n          break;\n        case 1:\n          image.src = images[name + "_hover"].src;\n          break;\n        case 2:\n          image.src = images[name + "_clicked"].src;\n          break;\n        case 3:\n          image.src = images[name + "_hover"].src;\n          break;\n      }\n\n  }\n</script>\n<!--\nEnd of the part generated by GIMP\n-->\n'
  237.             self.write(out)
  238.         
  239.  
  240.     
  241.     def row_start(self):
  242.         self.write('  <tr>')
  243.  
  244.     
  245.     def row_end(self):
  246.         self.write('</tr>\n')
  247.  
  248.     
  249.     def cell(self, src, width, height, row = 0, col = 0, skip_stub = False):
  250.         if isinstance(src, list):
  251.             prefix = 'images_%s' % self.image_prefix
  252.             self.images.append({
  253.                 'index': (row, col),
  254.                 'plain': src[0] })
  255.             out = ('    <td><a href="%s"><img alt="" src="%s" ' + 'style="width: %dpx; height: %dpx; border-width: 0px" \n') % (self.next_url(), src[0], width, height)
  256.             out += 'name="%d_%d" \n' % (row, col)
  257.             if len(src) >= 2:
  258.                 self.images[-1]['hover'] = src[1]
  259.                 out += '      onmouseout="exchange(this, \'%s\', 0);"\n' % prefix
  260.                 out += '      onmouseover="exchange(this, \'%s\', 1);"\n' % prefix
  261.             
  262.             if len(src) >= 3:
  263.                 self.images[-1]['clicked'] = src[2]
  264.                 out += '      onmousedown="exchange(this, \'%s\', 2);"\n' % prefix
  265.                 out += '      onmouseup="exchange(this, \'%s\', 3);"\n' % prefix
  266.             
  267.             out += '/></a></td>\n'
  268.         elif skip_stub:
  269.             out = ('    <td><img alt=" " src="%s" style="width: %dpx; ' + ' height: %dpx; border-width: 0px;"></td>') % (src, width, height)
  270.         else:
  271.             out = ('    <td><a href="#"><img alt=" " src="%s" ' + ' style="width: %dpx; height: %dpx; border-width: 0px;">' + '</a></td>') % (src, width, height)
  272.         self.write(out)
  273.  
  274.     
  275.     def parse_urls(self):
  276.         '''
  277.            This will parse any url targets in the href="XX" fields
  278.            of the given file and return then as a list
  279.         '''
  280.         import re
  281.         url_list = []
  282.         
  283.         try:
  284.             html_file = open(self.filename)
  285.             expr = re.compile('href\\=\\"([^\\"]*?)\\"')
  286.             url_list = expr.findall(html_file.read(262144))
  287.             html_file.close()
  288.         except:
  289.             pass
  290.  
  291.         return url_list
  292.  
  293.  
  294. register('python-fu-slice', N_('Cuts an image along its guides, creates images and a HTML table snippet'), "Add guides to an image. Then run this. It will cut along the guides,\n    and give you the html to reassemble the resulting images. If you\n    choose to generate javascript for onmouseover and clicked events, it\n    will use the lower three visible layers on the image for normal,\n    onmouseover and clicked states, in that order. If skip caps is\n    enabled, table cells on the edge of the table won't become animated,\n    and its images will be taken from the active layer.", 'Manish Singh', 'Manish Singh', '2003', _('_Slice...'), '*', [
  295.     (PF_IMAGE, 'image', 'Input image', None),
  296.     (PF_DRAWABLE, 'drawable', 'Input drawable', None),
  297.     (PF_DIRNAME, 'save-path', _('Path for HTML export'), os.getcwd()),
  298.     (PF_STRING, 'html-filename', _('Filename for export'), 'slice.html'),
  299.     (PF_STRING, 'image-basename', _('Image name prefix'), 'slice'),
  300.     (PF_RADIO, 'image-extension', _('Image format'), 'gif', (('gif', 'gif'), ('jpg', 'jpg'), ('png', 'png'))),
  301.     (PF_TOGGLE, 'separate-image-dir', _('Separate image folder'), False),
  302.     (PF_STRING, 'relative-image-path', _('Folder for image export'), 'images'),
  303.     (PF_SPINNER, 'cellspacing', _('Space between table elements'), 0, (0, 15, 1)),
  304.     (PF_TOGGLE, 'animate', _('Javascript for onmouseover and clicked'), False),
  305.     (PF_TOGGLE, 'skip-caps', _('Skip animation for table caps'), True)], [], pyslice, menu = '<Image>/Filters/Web', domain = ('gimp20-python', gimp.locale_directory))
  306. main()
  307.